package com.richman.view;

import java.awt.BorderLayout;
import org.json.*;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;


import com.richman.controller.DecreaseRatioClient;
import com.richman.view.BetCentralPane.MyTableModel;

public class ISNBetPanel extends JPanel{
	
	private  Vector<Object[]> detailsData = new Vector<Object[]>();	
	private  Vector<Object[]> detailsData1 = new Vector<Object[]>();	
	MyTableModel tableMode = new MyTableModel();
	JTable table = null;
	
	MyTableModel1 tableMode1 = new MyTableModel1();
	JTable table1 = null;
		
	public ISNBetPanel(){
		setLayout(new BorderLayout(0, 0));
		JPanel funBtnPane = new JPanel();
		
		JButton loaddataButton = new JButton("日结单");
		loaddataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//TODO:
				updateOutlineData(DecreaseRatioClient.betCtrMng.getOutlineData());
				
			}
		});
		
		loaddataButton.setBounds(20, 10, 60, 20);		
		funBtnPane.add(loaddataButton);		
		add(funBtnPane, BorderLayout.NORTH);
		
		
		
		
	    table = new JTable(tableMode);	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(120);	    
	    table.setRowHeight(30);	
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		
		
		//双击table事件
	    table.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {
            	
          	  if(e.getClickCount() == 2){

              	  int row =((JTable)e.getSource()).rowAtPoint(e.getPoint()); //获得行位置 
              	  

              	  if(Long.valueOf(detailsData.elementAt(row)[7].toString()) == 0){
              		  return;
              	  }
              	  
              	  
              	updateDetailsData(DecreaseRatioClient.betCtrMng.getDetailsData(detailsData.elementAt(row)[7].toString()));
              	  
          	  }else{
          		  return;
          	  }

            }
      });
		
		
	    table1 = new JTable(tableMode1);	    
	    table1.getColumnModel().getColumn(0).setPreferredWidth(120);
	    table1.setRowHeight(60);
	    table1.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		JScrollPane scrollPane1 = new JScrollPane(table1);
		add(scrollPane1, BorderLayout.SOUTH);
		
		table.getColumnModel().getColumn(0).setPreferredWidth(10);
		table1.getColumnModel().getColumn(0).setPreferredWidth(10);
		
	}
	
	
	public void updateDetailsData(String dataStr) {
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			detailsData1.clear();
			if(dataStr != null) {
				JSONArray ja = new JSONArray(dataStr);
				for(int i = 0; i < ja.length(); i++) {
					JSONObject jb = ja.getJSONObject(i);
					Object [] objdata = new Object[8];
					objdata[0] = i + 1;
					
					JSONObject jb1 = jb.getJSONObject("selectionDetail");
					
					objdata[1] = "<html>" + df.format(jb.getLong("betDate")) + "<br/>" + "编号:" + jb.getLong("betId");//jb.getString("betDateServer");
					objdata[2] = jb1.getString("marketTypeName");
					if(jb1.getString("marketTypeName").contains("滚球")) {
						objdata[3] = "<html>" + jb1.getString("leagueName") + "<br/>" 
								+ jb1.getString("eventName") + "<br/>" 
								+ jb1.getString("name") +  "[" + jb1.getString("handicap") + "@" + jb.getString("score") + "]" + "@" + jb1.getString("odds");
					}else {
						objdata[3] = "<html>" + jb1.getString("leagueName") + "<br/>" 
								+ jb1.getString("eventName") + "<br/>" 
								+ jb1.getString("name") +  "[" + jb1.getString("handicap") + "]" + "@" + jb1.getString("odds");
					}
					
					objdata[4] = jb.getDouble("stake");
					objdata[5] = jb.getDouble("winLoss");
					objdata[6] = jb.getDouble("commission");
					objdata[7] = "<html>" + jb.getString("status");

					detailsData1.add(objdata);
				}
			}else {
				JOptionPane.showMessageDialog(null,"投注数据获取失败，请稍后");
			}
			
			tableMode1.updateTable();
			
		}catch(Exception e) {
			e.printStackTrace();			
		}
		

	}
	
	
	public void updateOutlineData(String dataStr) {
		
		try {

			detailsData.clear();
			if(dataStr != null) {
				JSONArray ja = new JSONArray(dataStr);
				for(int i = 0; i < ja.length(); i++) {
					JSONObject jb = ja.getJSONObject(i);
					Object [] objdata = new Object[8];
					objdata[0] = i + 1;
					if(jb.getLong("statementDate") > 0) {
						objdata[1] = jb.getString("statementDateFormatted");
						objdata[3] = jb.getDouble("stake");
						objdata[4] = jb.getDouble("creditDebit");
						objdata[5] = jb.getDouble("commission");
						objdata[7] = jb.getLong("statementDate");
					}else {
						objdata[1] = "";
						objdata[3] = "";
						objdata[4] = "";
						objdata[5] = "";
						
						objdata[7] = 0;
					}
					
					objdata[2] = jb.getString("remark");
					

					objdata[6] = jb.getDouble("balance");
					detailsData.add(objdata);
				}
			}else {
				JOptionPane.showMessageDialog(null,"日结单数据获取失败，请稍后");
			}
			
			tableMode.updateTable();
			
		}catch(Exception e) {
			e.printStackTrace();			
		}
		

	}
	
	
	
    public class MyTableModel1 extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 99L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
		
        String[] columnNames =  
        { "序号", "时间",  "种类", "详情", "注额", "赢/输", "佣金", "状态"};
        
       // { "序号", "联赛", "时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率","1","2","时间"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel1()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData1){
	    		return 0;
	    	}
            return detailsData1.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	
        	if(detailsData1.size() == 0) {
        		return null;
        	}
        	
        	return detailsData1.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
        	detailsData1.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
	
	
	
    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
		
        String[] columnNames =  
        { "序号", "日期",  "备注", "注额", "收入/支出", "佣金", "余额"};
        
       // { "序号", "联赛", "时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率","1","2","时间"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	
        	if(detailsData.size() == 0) {
        		return null;
        	}
        	
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
}
